package com.example.switchbutton;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2018/6/15.
 */

public class SwitchButton extends ViewGroup {

    private int width;
    private int height;

    private int minX;

    public SwitchButton(Context context) {
        this(context,null);
    }

    public SwitchButton(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SwitchButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //zhong
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
        int childCount = getChildCount();
        minX = width/childCount;
        setMeasuredDimension(width,height);
        Log.e("onLayout", "width=="+ width +",,,,height=="+ height+",,minX=="+minX);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childAt = getChildAt(i);
            int width1 = childAt.getMeasuredWidth();
            childAt.layout((minX/2-width1/2)+minX*i,height/2-width1/2,(minX/2+width1/2)+minX*i,height-(height-width1)/2);
        }
    }

}
